package com.fr.schedule.trigger;

import com.fr.base.core.ComparatorUtils;
import com.fr.base.core.DateUtils;
import com.fr.base.core.json.JSONException;
import com.fr.base.core.json.JSONObject;
import com.fr.base.xml.XMLPrintWriter;
import com.fr.base.xml.XMLableReader;
import com.fr.schedule.dao.FieldMapper;
import com.fr.schedule.dao.ObjectMapper;
import com.fr.schedule.dao.ValueFieldMapper;
import com.fr.third.org.quartz.SimpleTrigger;
import com.fr.third.org.quartz.Trigger;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.ParseException;
import java.util.Date;
import java.util.TimeZone;
import oracle.sql.TIMESTAMP;

public class OnceITrigger
  implements ITrigger
{
  public static final String TABLE_NAME = "fr_once_itrigger";
  public static final int IMMEDIATE = 1;
  public static final int ON = 2;
  private long id = -1L;
  private long version;
  private String timeZoneID = TimeZone.getDefault().getID();
  private long startType = 1L;
  private Date startTime;
  public static final ObjectMapper OBJECT_MAPPER = new ObjectMapper("fr_once_itrigger", new FieldMapper[] { new FieldMapper("id", 4, "id"), new FieldMapper("timeZoneID", 12, "timezone_id"), new FieldMapper("startType", 4, "start_type"), new ValueFieldMapper("startTime", 93, "startTime")
  {
    public Object field2Value(Object paramObject)
    {
      return paramObject;
    }

    public Object value2Field(Object paramObject)
    {
      if (paramObject instanceof Timestamp)
        return new Date(((Timestamp)paramObject).getTime());
      if (paramObject instanceof TIMESTAMP)
        try
        {
          return new Date(((TIMESTAMP)paramObject).timestampValue().getTime());
        }
        catch (SQLException localSQLException)
        {
          localSQLException.printStackTrace();
        }
      return paramObject;
    }
  }
   });

  public OnceITrigger()
  {
  }

  public OnceITrigger(int paramInt, Date paramDate)
  {
    this.startType = paramInt;
    if (paramDate != null)
      this.startTime = paramDate;
  }

  public static OnceITrigger analyzeJSON(JSONObject paramJSONObject)
  {
    OnceITrigger localOnceITrigger;
    try
    {
      localOnceITrigger = null;
      long l = paramJSONObject.getLong("startType");
      Date localDate = null;
      if (l == 2L)
      {
        String str = paramJSONObject.getString("startTime");
        try
        {
          localDate = DateUtils.DATETIMEFORMAT2.parse(str);
        }
        catch (ParseException localParseException)
        {
          localParseException.printStackTrace();
        }
      }
      localOnceITrigger = new OnceITrigger((int)l, localDate);
      if (paramJSONObject.has("id"))
        localOnceITrigger.setId(paramJSONObject.getLong("id"));
      return localOnceITrigger;
    }
    catch (JSONException localJSONException)
    {
      localJSONException.printStackTrace();
    }
    return null;
  }

  public JSONObject createJSONConfig()
  {
    JSONObject localJSONObject = new JSONObject();
    try
    {
      localJSONObject.put("id", getId());
      localJSONObject.put("timeZoneID", getTimeZoneID());
      localJSONObject.put("startType", getStartType());
      if (getStartTime() != null)
      {
        Date localDate = getStartTime();
        String str = DateUtils.DATETIMEFORMAT2.format(localDate);
        localJSONObject.put("startTime", str);
        localJSONObject.put("recurrenceType", "once");
      }
    }
    catch (JSONException localJSONException)
    {
      localJSONException.printStackTrace();
    }
    return localJSONObject;
  }

  public Trigger createTrigger()
  {
    if (getStartType() == 1L)
      return new SimpleTrigger("trigger_" + getId() + "_" + getITriggerIdPrefix(), "ReportJobs");
    if ((getStartType() == 2L) && (getStartTime() != null))
      return new SimpleTrigger("trigger_" + getId() + "_" + getITriggerIdPrefix(), "ReportJobs", getStartTime());
    throw new IllegalStateException("The statemenet is illegal.");
  }

  public int getITriggerIdPrefix()
  {
    return 1;
  }

  public void setId(long paramLong)
  {
    this.id = paramLong;
  }

  public long getId()
  {
    return this.id;
  }

  public void setVersion(long paramLong)
  {
    this.version = paramLong;
  }

  public long getVersion()
  {
    return this.version;
  }

  public void setTimeZoneID(String paramString)
  {
    this.timeZoneID = paramString;
  }

  public String getTimeZoneID()
  {
    return this.timeZoneID;
  }

  public void setStartType(long paramLong)
  {
    this.startType = paramLong;
  }

  public long getStartType()
  {
    return this.startType;
  }

  public void setStartTime(Date paramDate)
  {
    this.startTime = paramDate;
  }

  public Date getStartTime()
  {
    return this.startTime;
  }

  public void readXML(XMLableReader paramXMLableReader)
  {
  }

  public void writeXML(XMLPrintWriter paramXMLPrintWriter)
  {
  }

  public boolean equals(Object paramObject)
  {
    if (!(paramObject instanceof OnceITrigger))
      return false;
    OnceITrigger localOnceITrigger = (OnceITrigger)paramObject;
    return ((localOnceITrigger.getId() == getId()) && (localOnceITrigger.getVersion() == getVersion()) && (ComparatorUtils.equals(localOnceITrigger.getStartTime(), getStartTime())) && (localOnceITrigger.getStartType() == getStartType()));
  }

  public Object clone()
    throws CloneNotSupportedException
  {
    OnceITrigger localOnceITrigger = (OnceITrigger)super.clone();
    if (getStartTime() != null)
      localOnceITrigger.setStartTime((Date)getStartTime().clone());
    return localOnceITrigger;
  }
}